<?php

class UsersAdminAbstractList extends UsersAbstractList
{

    protected $simpleusers = true;
    protected $new_fiscal_id;
    protected $new_name;
    protected $new_first_surname;
    protected $new_second_surname;
    protected $new_username;
    protected $new_password;
    protected $new_level;

    function setNewUserData( $fiscal_id, $name, $first_surname, $second_surname,
            $username, $password, $repassword, $level )
    {
        $this->new_fiscal_id = $fiscal_id;
        $this->new_name = $name;
        $this->new_first_surname = $first_surname;
        $this->new_second_surname = $second_surname;
        $this->new_username = $username;
        $this->new_level = $level;

        if ( empty( $name ) ) $this->errors[ ] = 'name';

        if ( empty( $first_surname ) ) $this->errors[ ] = 'first_surname';

        if ( empty( $fiscal_id ) ) $this->errors[ ] = 'fiscal_id';

        else if ( !UserNo::control_nif( $fiscal_id ) && !UserNo::control_nie( $fiscal_id ) )
                $this->errors[ ] = 'fiscal_id';

        else {
            $res = MyLink::$default->get_rows( 'SELECT id FROM users WHERE fiscal_id=' . MyLink::quote( $fiscal_id ),
                            true )->getField( 0 );
            if ( !empty( $res ) ) $this->errors[ ] = 'fiscal_id';
        }

        if ( empty( $username ) ) $this->errors[ ] = 'username';

        $res = MyLink::$default->get_rows( 'SELECT id FROM users WHERE username=' . MyLink::quote( $username ),
                        true )->getField( 0 );
        if ( !empty( $res ) ) $this->errors[ ] = 'username';

        if ( empty( $password ) ) $this->errors[ ] = 'password';

        if ( empty( $repassword ) ) $this->errors[ ] = 'repassword';

        if ( $password != $repassword ) $this->errors[ ] = 'repassword';
    }

    function handlePOST( Event $e )
    {
        if ( $e->tag == 'crear' ) {


            $_POST[ 'name' ] = trim( preg_replace( '/\s+?/', ' ',
                            $_POST[ 'name' ] ) );
            $_POST[ 'first_surname' ] = trim( preg_replace( '/\s+?/', ' ',
                            $_POST[ 'first_surname' ] ) );
            $_POST[ 'second_surname' ] = trim( preg_replace( '/\s+?/', ' ',
                            $_POST[ 'second_surname' ] ) );
            $_POST[ 'fiscal_id' ] = strtr( $_POST[ 'fiscal_id' ],
                    array( '.' => '', '-' => '', ' ' => '' ) );


            $this->_setNewUserData( $_POST[ 'fiscal_id' ], $_POST[ 'name' ],
                    $_POST[ 'first_surname' ], $_POST[ 'second_surname' ],
                    $_POST[ 'username' ], $_POST[ 'password' ],
                    $_POST[ 'repassword' ],
                    isset( $_POST[ 'level' ] ) ? $_POST[ 'level' ] : ''  );

            $e->cancelBubble();
        } else if ( $e->tag == 'eliminar' ) { //cuando se elimina un usuario se cancela el evento en la lista
            $e->cancelBubble();
        }
    }

    function doPOST( Event $e )
    {
        if ( $e->tag == 'crear' ) $this->crear();
    }

}

?>
